
package com.yuan.servlet;

import com.yuan.hibernate.员工信息表;
import com.yuan.hibernate.员工信息表DAO;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class getphotoservlet extends BaseHttpServlet {
    private 员工信息表 m员工信息表 = null;
    private 员工信息表DAO m员工信息表DAO = null;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        super.doGet(request, response);
        m员工信息表DAO = new 员工信息表DAO();
        int userid = Integer.parseInt(request.getParameter("id"));
        m员工信息表 = m员工信息表DAO.findById(userid);
        String temp = request.getSession().getServletContext().getRealPath("/") + "temp"; // 临时目录
        File mFile = null;
        mFile = new File(temp);
        if (!mFile.exists())
        {
            mFile.mkdirs();
        }
        String loadpath = request.getSession().getServletContext().getRealPath("/") + "Image"; // 上传文件存放目录
        System.out.println(loadpath);
        mFile = new File(loadpath);
        if (!mFile.exists())
        {
            mFile.mkdirs();
        }
        DiskFileUpload fu = new DiskFileUpload();
        fu.setSizeMax(100 * 1024 * 1024); // 设置允许用户上传文件大小,单位:字节
        fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:字节
        fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
        // 开始读取上传信息
        int index = 0;
        List<?> fileItems = null;
        try {
            fileItems = fu.parseRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Iterator<?> iter = fileItems.iterator(); // 依次处理每个上传的文件
        while (iter.hasNext())
        {
            FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表单信息
            if (!item.isFormField())
            {
                String name = item.getName();// 获取上传文件名,包括路径
                if ((name == null || name.equals("")))
                    continue;
                name = (new Date()).getTime() + name + index;
                index++;
                try {
                    File fNew = new File(loadpath, name + ".jpg");
                    item.write(fNew);
                    m员工信息表.set照片(name + ".jpg");
                    m员工信息表DAO.update(m员工信息表);
                    System.out.println("图片存储绝对路径：" + name + ".jpg");
                    mResponse = "上传成功";
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    mResponse = "上传失败";
                }
            }
        }
        closeAndClear();
    }

    protected synchronized void closeAndClear() {
        m员工信息表 = null;
        m员工信息表DAO = null;
        super.closeAndClear();
    }
}
